// pages/editAddress/editAddress.js
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Tname: '编辑收货地址',
    checked: true,
    show: false,
    region: ['四川省', '成都市', '武侯区'],
    customItem: '不限',
    status: '',
    indexNum: 0,
  },
  // 地区选择
  bindRegionChange: function (e) {
    let tmpArr = e.detail.value
    let province = tmpArr[0]
    let city = tmpArr[1]
    let area = tmpArr[2]
    this.setData({
      region: e.detail.value,
      province,
      city,
      area,
      indexNum: 1
    })
  },

  onChange(event) {
    console.log(event.detail)
    this.setData({
      checked: event.detail,
    });
    if (event.detail == true) {
      this.setData({
        status: 1
      })
    } else {
      this.setData({
        status: 0
      })
    }
  },
  // popup开
  showPopup() {
    this.setData({
      show: true
    });
  },
  // popup关闭
  onClose() {
    this.setData({
      show: false
    });
  },
  // input监听
  inputLister(e) {
    let inputValue = e.currentTarget.dataset.input
    let notNull = e.detail.value
    if (notNull == '') {
      wx.showToast({
        title: '输入框不能为空',
        icon: 'none'
      })
    }
    this.setData({
      [inputValue]: e.detail.value,
      notNull
    })
  },
  confirmEdit(e) {
    let _this = this
    let status = _this.data.status
    let postcode = _this.data.postcode
    let position = _this.data.position
    let phone = _this.data.phone
    let name = _this.data.name
    let address_id = _this.data.addressid
    let notNull = _this.data.notNull
    // let area1 = _this.data.province
    // let area2 = _this.data.city
    // let area3 = _this.data.area
    if (address_id == '' || address_id == undefined) {
      console.log(this.data.area1)
      http.postHttp('/user_address_save', {
        province: _this.data.area1,
        city:_this.data.area2,
        area: _this.data.area3,
        postcode: postcode,
        position: position,
        phone: phone,
        name: name,
        status: status
      }, {
        token: wx.getStorageSync('token')
      }).then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        wx.navigateBack({
          delta: 1
        })
      })
    } else {
      http.postHttp('/user_address_save', {
        province: this.data.area1,
        city: this.data.area2,
        area: this.data.area3,
        postcode: postcode,
        position: position,
        phone: phone,
        name: name,
        status: status,
        address_id: address_id
      }, {}).then(res => {
        wx.showToast({
          title: '修改地址成功',
          icon: 'none'
        })
        wx.navigateBack({
          delta: 1
        })
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let tmpArr = this.data.region
    let area1 = tmpArr[0]
    let area2 = tmpArr[1]
    let area3 = tmpArr[2]
    let addressid = options.addressid
    let name = options.name
    let phone = options.phone
    let postcode = options.postcode
    let province = options.province
    let city = options.city
    let area = options.area
    let position = options.position
    this.setData({
      area1,
      area2,
      area3,
      addressid,
      name,
      phone,
      postcode,
      province,
      city,
      area,
      position
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})